package com.xinfu.newproject.adapter.home_adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xinfu.newproject.R;
import com.xinfu.newproject.bean.home.bean.GetAppIconBean;

import java.util.List;

import cn.bingoogolapple.badgeview.BGABadgeFrameLayout;

public class YiGridViewTwoAdapter extends BaseAdapter {
    private List<GetAppIconBean.ResultBean.MenuListBean> mDatas;
    private LayoutInflater inflater;
    private Context context;
    private Integer integer;
    /**
     * 页数下标,从0开始(当前是第几页)
     */
    private int curIndex;
    /**
     * 每一页显示的个数
     */
    private int pageSize;

    public YiGridViewTwoAdapter(Context context, List<GetAppIconBean.ResultBean.MenuListBean> mDatas, int curIndex, int pageSize) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.mDatas = mDatas;
        this.curIndex = curIndex;
        this.pageSize = pageSize;
    }

    public void addTextSize(Integer integer) {
        this.integer = integer;
        notifyDataSetChanged();
    }

    /**
     * 先判断数据集的大小是否足够显示满本页？mDatas.size() > (curIndex+1)*pageSize,
     * 如果够，则直接返回每一页显示的最大条目个数pageSize,
     * 如果不够，则有几项返回几,(mDatas.size() - curIndex * pageSize);(也就是最后一页的时候就显示剩余item)
     */
    @Override
    public int getCount() {
        return mDatas.size() > (curIndex + 1) * pageSize ? pageSize : (mDatas.size() - curIndex * pageSize);

    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position + curIndex * pageSize);
    }

    @Override
    public long getItemId(int position) {
        return position + curIndex * pageSize;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_gridview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tv = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.imageView);
            viewHolder.frame_bga = convertView.findViewById(R.id.framelayout_bga);
            viewHolder.sizeTv = convertView.findViewById(R.id.item_gridview_size);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        /**
         * 在给View绑定显示的数据时，计算正确的position = position + curIndex * pageSize，
         */
        int pos = position + curIndex * pageSize;
        GetAppIconBean.ResultBean.MenuListBean entity = mDatas.get(pos);
        viewHolder.tv.setText(entity.getName());
        if (entity.getName().equals("报修维修") && integer != null) {
            if (integer > 0) {
                viewHolder.sizeTv.setText(integer + "");
                viewHolder.sizeTv.setVisibility(View.VISIBLE);
            } else {
                viewHolder.sizeTv.setVisibility(View.GONE);
            }
        } else {
            viewHolder.sizeTv.setVisibility(View.GONE);
        }
        Glide.with(context).load(entity.getIcon())
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .thumbnail(0.3F)
                .dontAnimate()
                .into(viewHolder.iv);
        //hot
      /*  if (!TextUtils.isEmpty(entity.hotype)) {
            viewHolder.frame_bga.showTextBadge(entity.hotype);
        } else {
            viewHolder.frame_bga.hiddenBadge();
        }*/
        viewHolder.frame_bga.hiddenBadge();
        return convertView;
    }


    static class ViewHolder {
        TextView tv;
        ImageView iv;
        BGABadgeFrameLayout frame_bga;
        TextView sizeTv;
    }
}